using System;
using System.Xml;
using System.Xml.Schema;

namespace Sedna.Core.Event
{
    public abstract class BaseEvent : IEvent
    {
        protected const String REFERENCED_NS = "http://yourdomain.com/ReferencedData";
        protected const String EVENT_NS = "http://yourdomain.com/BusinessEvent";
        //protected const String EVENT_SOURCE_NS = "http://yourdomain.com/BusinessEvent/EventSource";
        protected const String MESSAGES_NS = "http://yourdomain.com/BusinessEvent";

        protected const String REFERENCED_PREFIX = "rd";
        //protected const String EVENT_SOURCE_PREFIX = "es";
        protected const String MESSAGES_PREFIX = "es";

        protected const String REFERENCED_SCHEMA = "~/xsd/ReferencedData.xsd";
        protected const String EVENT_SCHEMA = "~/xsd/BusinessEvent.xsd";

        public BaseEvent()
        {}
        
        public BaseEvent(String name)
        {
            this.name = name;
        }
        
        private String name;

        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        /// <summary>
        /// The method should return new instance of XmlSchemaSet which sufficient for validating
        /// XML instances could be generated by event.
        /// </summary>
        public abstract XmlSchemaSet ValidateSchemaSet();                

        /// <summary>
        /// Implement this method for create appropriate XML instance of an event source.
        /// The implemetation must write full element with start and end tags.
        /// </summary>
        public abstract void WriteEventSourceXml(XmlWriter writer);

        public void WriteXml(XmlWriter writer)
        {
            writer.WriteAttributeString("name", Name);
            writer.WriteAttributeString("xmlns", "mstns", null, EVENT_NS);
            writer.WriteAttributeString("xmlns", REFERENCED_PREFIX,null,REFERENCED_NS);
            //writer.WriteAttributeString("xmlns",EVENT_SOURCE_PREFIX,null, EVENT_SOURCE_NS);
            
            writer.WriteElementString(
                "event-date",
                DateTime.Now.ToString("yyyy-MM-dd"));
            
            WriteEventSourceXml(writer);
        }
        
        protected static XmlQualifiedName EventTypeNameNewInstance()
        {
            return new XmlQualifiedName("business-event", EVENT_NS);
        }

        #region Must not be implemented methods

        public XmlSchema GetSchema()
        {
            throw new NotImplementedException();
        }

        public void ReadXml(XmlReader reader)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
